<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { systemQuickAddApi as systemApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
  })
  interface formQuert {
    id:number
    user_id:number
    uuid:number
    gitf_id:string
    number:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [{ required: true, message: "请输入用户", trigger: "blur" }],
    uuid: [{ required: true, message: "请输入用户", trigger: "blur" }],
    gitf_id: [{ required: true, message: "请输入礼物ID", trigger: "blur" }],
    number: [{ required: true, message: "请输入数量", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      const subform={
        user_id:form.user_id,
        // uuid:form.uuid,
        gift_id:form.gift_id,
        number:form.number,
        remark:form.remark
      }
      if(id) {
        await systemApi.updateSystems(id,subform).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await systemApi.addSystems(subform).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const { id=0, key='', type=0, value='', desc='' } = query
    if(id) {
      action.title = '编辑配置'
      Object.assign(form,{
        id, key, type, value, desc
      })
    } else {
      action.title = '新增配置'
    }
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="52%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID" prop="uuid">
          <serachUserid v-model:user_id="form.user_id" v-model:uuid="form.uuid" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="礼物ID" prop="giftId">
          <serachGift  v-model:gift_id="form.gift_id" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="数量" prop="count">
          <el-input v-model="form.number" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>